package com.zxw.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.zxw.config.SentinelBlockHandler;
import com.zxw.hystrix.TestCommand;
import com.zxw.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author jimin.jm@alibaba-inc.com
 * @date 2019/06/14
 */

@RestController
@RequestMapping("/bus")
public class BusinessController {

    @Autowired
    private BusinessService businessService;

    @Autowired
    private TestCommand testCommand;
    /**
     * 购买下单，模拟全局事务提交
     *
     * @return
     */
    @GetMapping
    @SentinelResource(value = "purchaseCommit",
    blockHandlerClass = SentinelBlockHandler.class,
    blockHandler = "handler")
    public String purchaseCommit() {
        try {
//            testCommand.execute();
            businessService.purchase("U100000", "C100000", 30);
        } catch (Exception exx) {
            return exx.getMessage();
        }
        return "全局事务提交";
    }

    /**
     * 购买下单，模拟全局事务回滚
     * 账户或库存不足
     *
     * @return
     */
    @RequestMapping("/purchase/rollback")
    public String purchaseRollback() {
        try {
            businessService.purchase("U100000", "C100000", 99999);
        } catch (Exception exx) {
            return exx.getMessage();
        }
        return "全局事务提交";
    }
}
